package com.forlove.app.qselect.friend.presenter;

import android.text.TextUtils;

import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.manager.FriendManager;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.friend.entity.ContactsFriendEntity;
import com.forlove.app.qselect.friend.entity.MineFriendListEntity;
import com.forlove.app.qselect.friend.entity.SearchFriendEntity;
import com.forlove.app.qselect.friend.service.IMineNewFriendService;
import com.forlove.app.qselect.friend.service.impl.MineNewFriendServiceImpl;
import com.forlove.app.qselect.friend.view.MineNewFriendView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yjl on 2017/10/23.
 */

public class MineNewFriendPresenter extends IBasePresenter<IMineNewFriendService> {
    private IMineNewFriendService mineNewFriendService;
    private MineNewFriendView mineNewFriendView;

    public MineNewFriendPresenter(MineNewFriendView mineNewFriendView) {
        this.mineNewFriendView = mineNewFriendView;
        if (mineNewFriendService == null) {
            mineNewFriendService = new MineNewFriendServiceImpl();
            setService(mineNewFriendService);
        }

    }

    public void loadAgreeFriendData(Map param) {
        mineNewFriendService.loadData(ServerManager.ACTION_AGREE_FRIEND, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "同意好友请求!");
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (commonEntity.state == NetworkManager.ResultCode.RESULT_SUCCESS.getValue()) {
                    if (mineNewFriendView != null) {
                        mineNewFriendView.showAgreeFriend(NetworkManager.ResultCode.RESULT_SUCCESS, "添加成功!");
                    }
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (mineNewFriendView != null) {
                        mineNewFriendView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    }
                } else {
                    if (mineNewFriendView != null) {
                        mineNewFriendView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });
    }

    public void loadDeleteFriendRecord(Map param) {
        mineNewFriendService.loadData(ServerManager.ACTION_DELETE_FRIEND_RECORD, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                mineNewFriendView.showDeleteRecordView(commonEntity);
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (mineNewFriendView != null) {
                        mineNewFriendView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                    }
                } else {
                    if (mineNewFriendView != null) {
                        mineNewFriendView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                    }
                }
            }
        });
    }


    public void loadContactFriend() {
        mineNewFriendService.loadData(ServerManager.ACTION_GET_CONTACTS_FRIEND, new HashMap(), new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                ContactsFriendEntity friendListEntity = new ContactsFriendEntity();
                friendListEntity.parseJSONString(ParseHelper.getString(result, "data"));
                mineNewFriendView.updateContactsFriendView(friendListEntity);
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    mineNewFriendView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    mineNewFriendView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                }
            }
        });
    }

    /**
     * 获取好友列表（1.01新接口）
     */
    public void loadFriendList() {
        mineNewFriendService.loadData(ServerManager.ACTION_FRIEND_LIST_NEW, new HashMap(), new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "好友列表:" + result);
                MineFriendListEntity mineFriendListEntity =
                        ParseHelper.getModel(result, "data", MineFriendListEntity.class);
                FriendManager.saveMineFriend(result);
                mineNewFriendView.updateFriendListView(mineFriendListEntity);
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    mineNewFriendView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    mineNewFriendView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                }
            }
        });
    }

    public void loadSearchFriend(Map param) {
        mineNewFriendService.loadData(ServerManager.ACTION_FRIEND_SEARCH, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                SearchFriendEntity searchFriendEntity = ParseHelper.getModel(result, "data", SearchFriendEntity.class);
                if (mineNewFriendView != null) {
                    mineNewFriendView.showScanViewData(searchFriendEntity);
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    mineNewFriendView.showErrorData(NetworkManager.ResultCode.RESULT_FAIL, commonEntity.message);
                } else {
                    mineNewFriendView.showErrorData(NetworkManager.ResultCode.RESULT_ERROR, "稍后重试!");
                }
            }
        });
    }
}
